﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LNBMS.BLL;
using LNBMS.DAL;
using LNBMS.Entity;
using System.Reflection;


namespace LNBMS.Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["remember"] != null)
                Response.Redirect(string.Format("Index.aspx?username={0}", Session["username"].ToString()));
        }

        protected void OnLoginClick(object sender, EventArgs e)
        {
            string username = Request.Form["name"].ToString();
            string password = Request.Form["password"].ToString();
            string remember = Request.Form["remember"] == null ? null : Request.Form["remember"].ToString();

            if (!string.IsNullOrEmpty(remember))
                Session["remember"] = remember;
            if (Judge(username, password))
            {
                Session["username"] = username;
                Session.Timeout = 10;
                Response.Redirect("Index.aspx");
            }
            else
                Response.Write("<script>alert('用户名或密码错误!')</script>");
        }

        /// <summary>
        /// 账号验证
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private bool Judge(string username, string password)
        {
            bool isLogin = false;
            ProvinceBLL provincebll = new ProvinceBLL();
            Province province = new Province();
            if (username == "admin")
            {
                province = provincebll.GetEntity(username);
                if (province.password == password)
                    isLogin = true;
            }
            else
            {
                switch (username.Length)
                {
                    case 4:
                        var citybll = new CityBLL();
                        var city = new City();
                        city = citybll.GetEntity(username);
                        if (city.password == password)
                            isLogin = true;
                        break;
                    case 6:
                        var countybll = new CountyBLL();
                        var county = new County();
                        county = countybll.GetEntity(username);
                        if (county.password == password)
                            isLogin = true;
                        break;
                    case 9:
                        var townbll = new TownBLL();
                        var town = new Town();
                        town = townbll.GetEntity(username);
                        if (town.password == password)
                            isLogin = true;
                        break;
                    case 12:
                        var villagebll = new VillageBLL();
                        var village = new Village();
                        village = villagebll.GetEntity(username);
                        if (village.password == password)
                            isLogin = true;
                        break;
                    default:
                        break;
                }
            }
            return isLogin;
        }
    }
}